package ps

import (
	influx "github.com/influxdata/influxdb1-client/v2"
	"github.com/shirou/gopsutil/v3/cpu"
	"github.com/sirupsen/logrus"
	"time"
	"tym.com/LogCollection/influxdb"
)

func getCpuPercent() {
	percent, _ := cpu.Percent(time.Second, false)
	packCpuPoints(percent[0])
}

func packCpuPoints(f float64) {
	monitorPoints, err := influx.NewBatchPoints(influx.BatchPointsConfig{
		Database:  "monitor",
		Precision: "s", //精度，默认ns
	})
	// 根据传入数据的类型插入数据
	tags := map[string]string{"cpu": "cpu0"}
	fields := map[string]interface{}{
		"cpu_percent": f,
	}

	pt, err := influx.NewPoint("cpu", tags, fields, time.Now())
	if err != nil {
		logrus.Error(err)
	}
	monitorPoints.AddPoint(pt)
	influxdb.PointsSubChan <- monitorPoints
	logrus.Info("ps: insert influx cpu")

}
